package com.cxh.rmsq.utils.dialog

import android.content.Context
import android.content.Intent
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.TextView
import androidx.annotation.NonNull
import com.blankj.utilcode.util.ActivityUtils.startActivity
import com.cxh.rmsq.R
import com.cxh.rmsq.common.Constant
import com.cxh.rmsq.ui.activity.WebViewActivity
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils
import kotlin.math.roundToInt
import kotlin.system.exitProcess


class LoginPopup(context: Context, flag: Int, click: onClick) : CenterPopupView(context) {

    interface onClick {
        fun click(s: String, s1: String, s2: String, s3: String, s4: String)
    }

    var func: onClick
    var flag: Int

    init {
        this.func = click
        this.flag = flag
    }

    override fun getImplLayoutId(): Int {
        return R.layout.login_popup
    }

    override fun onCreate() {
        super.onCreate()

        var tv_content = findViewById<TextView>(R.id.tv_content)
        var tv_reset = findViewById<TextView>(R.id.tv_reset)
        var tv_ok = findViewById<TextView>(R.id.tv_ok)
        tv_content.movementMethod = LinkMovementMethod.getInstance()
        tv_content.text = getClickableSpan()

        tv_reset.setOnClickListener {
            if (flag == 1) {// tuichu
                exitProcess(0)
            }
            dismiss()
        }
        tv_ok.setOnClickListener {
            func.click(
                "",
                "",
                "",
                "",
                ""
            )
            dismiss()
        }
    }

    // 最大高度为Window的0.85
    override fun getMaxHeight(): Int {
        return ((XPopupUtils.getAppHeight(context) * .95f).roundToInt())
    }

    private fun getClickableSpan(): SpannableString? {
        val txt = "为了更好的保护您的权利，同时遵守相关监管要求。如果点击同意并继续则代表您已阅读并同意《人脉社区用户协议》与《人脉社区隐私政策》。"
        val spannableString = SpannableString(txt)

        val span1 = object : ClickableSpan() {
            //重写该方法去掉下划线
            override fun updateDrawState(@NonNull ds: TextPaint) {
                ds.isUnderlineText = false
            }

            override fun onClick(widget: View) {
                val intent = Intent(
                    activity,
                    WebViewActivity::class.java
                )
                intent.putExtra(WebViewActivity.PARAMS_TITLE, "用户协议")
                intent.putExtra(WebViewActivity.PARAMS_URL, Constant.userAgreement)
                startActivity(intent)
            }
        }

        var start1 = txt.indexOf("《人脉社区用户协议》")
        var end1 = txt.indexOf("《人脉社区用户协议》") + 10
        //设置文字的点击事件
        spannableString.setSpan(span1, start1, end1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        //设置文字的前景色
        spannableString.setSpan(
            ForegroundColorSpan(resources.getColor(R.color.color_FF7217)),
            start1,
            end1,
            Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        )

        val span2 = object : ClickableSpan() {
            //重写该方法去掉下划线
            override fun updateDrawState(@NonNull ds: TextPaint) {
                ds.isUnderlineText = false
            }

            override fun onClick(widget: View) {
                val intent = Intent(
                    activity,
                    WebViewActivity::class.java
                )
                intent.putExtra(WebViewActivity.PARAMS_TITLE, "隐私政策")
                intent.putExtra(WebViewActivity.PARAMS_URL, Constant.privacyPolicy)
                startActivity(intent)
            }
        }

        var start2 = txt.indexOf("《人脉社区隐私政策》")
        var end2 = txt.indexOf("《人脉社区隐私政策》") + 10
        //设置文字的点击事件
        spannableString.setSpan(span2, start2, end2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        //设置文字的前景色
        spannableString.setSpan(
            ForegroundColorSpan(resources.getColor(R.color.color_FF7217)),
            start2,
            end2,
            Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        )
        return spannableString
    }
}